package football.modele;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Phase implements Serializable, java.lang.Comparable {

	private static final long serialVersionUID = 1546069369785747128L;
	private int idPhase;
	private Set<Match> listeMatchs;
	private Tournoi tournoi;
	private String nomPhase;

	public Phase() {
		super();
		this.listeMatchs = new HashSet<Match>();
	}

	public Phase(Tournoi tournoi, String nomPhase) {
		super();
		this.tournoi = tournoi;
		this.listeMatchs = new HashSet<Match>();
		this.nomPhase = nomPhase;
	}

	@Id
	@GeneratedValue
	public int getIdPhase() {
		return idPhase;
	}

	public void setIdPhase(int idPhase) {
		this.idPhase = idPhase;
	}

	@OneToMany(mappedBy = "phase", cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	public Set<Match> getListeMatchs() {
		return listeMatchs;
	}

	public void setListeMatchs(Set<Match> listeMatchs) {
		this.listeMatchs = listeMatchs;
	}

	@ManyToOne
	public Tournoi getTournoi() {
		return tournoi;
	}

	public void setTournoi(Tournoi tournoi) {
		this.tournoi = tournoi;
	}

	public String getNomPhase() {
		return nomPhase;
	}

	public void setNomPhase(String nomPhase) {
		this.nomPhase = nomPhase;
	}

	@Override
	public int compareTo(Object other) {
		int nombre1 = ((Phase) other).getIdPhase();
		int nombre2 = this.getIdPhase();
		if (nombre1 > nombre2)
			return -1;
		else if (nombre1 == nombre2)
			return 0;
		else
			return 1;
	}
	
	public boolean validerPhase(Phase p) {
		if (p.getListeMatchs().size() == 1)
			return false;
		for (Match m : p.getListeMatchs()) {
			if (!m.isMatchTermine()) {
				return false;
			}
		}
		return true;
	}

}
